<template>
    <div class="fillcontain">
        <head-top></head-top>
        <div class="table_container">
            <br />
            <el-row :gutter="20 " class="demo-autocomplete">
                <el-col :span="7" :offset="1">
                    <!-- Form -->
                    <el-button type="primary" @click="dialogFormVisible = true">添加客户类型</el-button>

                    <el-dialog title="您正在进行添加操作" :visible.sync="dialogFormVisible" width="28%" center="">
                        <hr />
                        <el-form :model="addForm">
                            <el-form-item label="类型名称:" :label-width="formLabelWidth">
                                <el-col :span="18">
                                    <el-input v-model="addForm.configtypename" autocomplete="off"></el-input>
                                </el-col>
                            </el-form-item>
                            <el-form-item label="是否启用:" :label-width="formLabelWidth">
                                <el-col :span="18">
                                    <el-select v-model="addForm.isstart">
                                        <el-option label="启用" :value="1"></el-option>
                                        <el-option label="不启用" :value="0"></el-option>
                                    </el-select>
                                </el-col>
                            </el-form-item>
                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button type="primary" @click="insert">保 存</el-button>
                            <el-button @click="dialogFormVisible = false">取 消</el-button>
                        </div>
                    </el-dialog>
                </el-col>
            </el-row>
            <br />
            <br />
            <template>
                <el-table :data="SystemConfigs" height="400" border style="width: 100%">
                    <el-table-column prop="id" align="center" label="序号">
                    </el-table-column>
                    <el-table-column prop="configtypename" align="center" label="配置类型">
                    </el-table-column>
                    <el-table-column align="center" label="是否启用">
                        <template slot-scope="scope">{{scope.row.isstart==1?'启用':'未启用'}}</template>
                    </el-table-column>
                    <el-table-column align="center" label="操作">
                        <template slot-scope="scope">
                            <el-button type="infor" @click="updateinit(scope.row)">修改</el-button>
                            <el-dialog title="您正在进行修改操作" :visible.sync="updateFormVisible" width="28%" center="">
                                <hr />
                                <el-form :model="updateForm">
                                    <el-form-item label="类型名称:" :label-width="formLabelWidth">
                                        <el-col :span="18">
                                            <el-input v-model="updateForm.configtypename" autocomplete="off"></el-input>
                                        </el-col>
                                    </el-form-item>
                                    <el-form-item label="是否启用:" :label-width="formLabelWidth">
                                        <el-col :span="18">
                                            <el-select v-model="updateForm.isstart">
                                                <el-option label="启用" :value="1"></el-option>
                                                <el-option label="不启用" :value="0"></el-option>
                                            </el-select>
                                        </el-col>
                                    </el-form-item>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button type="primary" @click="update">保 存</el-button>
                                    <el-button @click="updateFormVisible = false">取 消</el-button>
                                </div>
                            </el-dialog>
                            <el-button type="danger" @click="confirmDel(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>
        </div>
    </div>
</template>

<script>
    import headTop from '../components/headTop'
    import {
        queryByConfigType,
        insertSystemConfig,deleteSystemConfig,updateSystemConfig
    } from '@/api/getData'
    import {
        mapState
    } from 'vuex'
    import {
        baseUrl,
        baseImgPath
    } from '@/config/env'

    export default {
        data() {
            return {
                updateForm:{},
                SystemConfigs: [],
                updateFormVisible: false,
                dialogFormVisible: false,
                addForm: {
                    configtypename: '',
                    isstart: 1,
                    configtype: 5
                },
                formLabelWidth: '120px'

            }
        },
        components: {
            headTop,
        },
        created() {
            this.getData();
        },
        computed: {
            ...mapState(['adminInfo']),
        },
        methods: {
            async getData() {
                try {
                    const res = await queryByConfigType(5);
                    console.log(res)
                    this.SystemConfigs = res;
                } catch (err) {
                    console.log('获取数据失败', err);
                }
            },
            async insert() {
                this.dialogFormVisible = false;
                const res = await insertSystemConfig(this.addForm);
                if (res.status == 1) {
                    this.$message({

                        type: 'success',

                        message: res.message

                    });
                    this.getData();
                } else {
                    this.$message({

                        type: 'error',

                        message: res.message

                    });
                }
            },
             confirmDel(id){
               this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                          confirmButtonText: '确定',
                          cancelButtonText: '取消',
                          type: 'warning'
                        }).then(() => {
                          this.del(id);
                        }).catch(() => {
                          this.$message({
                            type: 'info',
                            message: '已取消删除'
                          });
                        });
            },
            async del(id){
                const res = await deleteSystemConfig({id:id});
                if (res.status == 1) {
                    this.$message({

                        type: 'success',

                        message: res.message

                    });
                    this.getData();
                } else {
                    this.$message({

                        type: 'error',

                        message: res.message

                    });
                }

            },
            updateinit(config){
                this.updateForm=config;
                this.updateFormVisible = true;
            },
            async update(){
                this.updateFormVisible = false;
                const res = await updateSystemConfig(this.updateForm);
                if (res.status == 1) {
                    this.$message({

                        type: 'success',

                        message: res.message

                    });
                    this.getData();
                } else {
                    this.$message({

                        type: 'error',

                        message: res.message

                    });
                }
            }
        }
    }
</script>

<style lang="less">
    @import '../style/mixin';
</style>
